package com.mall;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

/**
 * idcard加密解密方式
 */
public class AESUtilIdcard {
    public static String AESEncode(String content) {
        try {
            SecretKeySpec secretKey = new SecretKeySpec("IDSecretKeyByZZX".getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encrypted = cipher.doFinal(content.getBytes());
            return new String(Base64.getEncoder().encode(encrypted), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String AESDecode(String AES_code) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec("IDSecretKeyByZZX".getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        try {
            byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(AES_code));
            return new String(decrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String s = "330424199308230014";

        System.out.println("s:" + s);

        String s1 = AESEncode(s);
        System.out.println("s1:" + s1);

        try {
            System.out.println("s2:"+AESDecode("CkseQVexTz1pX1w+EprD9MP1cOrtZFwMuTaDL3yNLvQ="));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
